package com.cn.aspect;


import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;

 





import com.cn.annotation.MethodRedisLimit;

import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;

/**
 * Function: general annotation
 *
 * @author crossoverJie
 *         Date: 27/04/2018 15:50
 * @since JDK 1.8
 */
@Aspect
@Component
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class CommonAspect {

    private static Logger logger = LoggerFactory.getLogger(CommonAspect.class);

 

    @Pointcut("@annotation(com.cn.annotation.MethodRedisLimit)")
    private void check(){}

    @Before("check()")
    public void before(JoinPoint joinPoint) throws Exception {
         
    	Signature   signature = joinPoint.getSignature();
    	 MethodSignature methodSignature = (MethodSignature)signature;
    	 Method targetMethod = methodSignature.getMethod();
    	 Class clazz = targetMethod.getClass(); 
    	 
    	 MethodRedisLimit mr = (MethodRedisLimit) targetMethod.getAnnotation(MethodRedisLimit.class);
    	 System.out.println(mr.fallback());
    	 System.out.println( mr.limitKey() );
    	 Method sd = joinPoint.getTarget().getClass().getDeclaredMethod(mr.fallback(), targetMethod.getParameterTypes());
    	 Object result = sd.invoke(joinPoint.getTarget(), joinPoint.getArgs());
    	 
    }
}
